<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
class Visitors extends Model{
		//protected $updateTime = 'visitors_addtime';
		protected $auto = ['visitors_ip','visitors_addtime'];
		
	    protected function setVisitorsIpAttr(){
		    return bindec(decbin(ip2long(request()->ip())));
	    }
	    protected function setVisitorsAddtimeAttr($value){
		    return time();
	    }		
    //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='user_'.$param['order'].' desc';
		}		
		if(empty($tag['order'])){
			$tag['order']='visitors_addtime desc';
		}
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		$where['user_status']=['eq',1];
        $where['user_admin']=['eq',0];	
	    if(!empty($tag['uid'])) {
		    $uid = explode(',',$tag['uid']);
		    if(count($uid)>1) {
			    $where['visitors_uid'] = array('in',$tag['uid']);
		    }else{
			    $where['visitors_uid'] = array('eq',$uid[0]);
		    }
			$rs=db('Visitors')->alias('v')->join('user u','u.user_id = v.visitors_userid','LEFT');
	    }		
	    if(!empty($tag['userid'])) {
		    $ids = explode(',',$tag['userid']);
		    if(count($ids)>1) {
			    $where['visitors_userid'] = array('in',$tag['ids']);
		    }else{
			    $where['visitors_userid'] = array('eq',$ids[0]);
		    }
			$rs=db('Visitors')->alias('v')->join('user u','u.user_id = v.visitors_uid','LEFT');
	    }	
        if(!empty($tag['page'])){
			if(config('route')){
				$route=config('route');
			}else{
				$route=$GLOBALS['route'];
			}
            $url=urldecode(zanpian_user_url($route,config('params')));
		    $data = $rs->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = $rs->where($where)->order($tag['order'])->limit($limit_str)->select();
		}
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_visitors')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }		
		return $data;
    } 		
	    //更具用户ID获取访客
		public function add($uid='',$userid=''){
		    $visitors = model('Visitors');
            $data['visitors_uid']=$uid;			
		    $data['visitors_userid']=$userid;
			if($visitors->validate(true)->save($data)==false){
				$visitors->where($data)->update(['visitors_addtime' =>time(),'visitors_ip'=>bindec(decbin(ip2long(request()->ip())))]);
			}
	    }	   
   
	
}